package com.blink.curator.recipe.distributed;

import com.blink.base.BaseTestSample;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
import org.junit.jupiter.api.Test;

/**
 * <Description> 主节点选举</Description>
 * <ClassName> MasterSelectSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 17:47
 */
public class MasterSelectSample extends BaseTestSample {

    static String master_path = "/curator_recipes_master_path";

    @Test
    public void testMasterSelect() throws Exception {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                CuratorFramework client1 = getZkCurator();
                client1.start();
                LeaderSelector selector1 = new LeaderSelector(client1,
                        master_path,
                        new LeaderSelectorListenerAdapter() {
                            @Override
                            public void takeLeadership(CuratorFramework client) throws Exception {
                                logger.info("t1成为Master角色");
                                sleep();
                                logger.info("t1完成Master操作，释放Master权利");
                            }
                        });
                selector1.autoRequeue();
                selector1.start();
            }
        },"t1");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                CuratorFramework client2 = getZkCurator();
                client2.start();
                LeaderSelector selector2 = new LeaderSelector(client2,
                        master_path,
                        new LeaderSelectorListenerAdapter() {
                            @Override
                            public void takeLeadership(CuratorFramework client) throws Exception {
                                logger.info("t2成为Master角色");
                                sleep();
                                logger.info("t2完成Master操作，释放Master权利");
                            }
                        });
                selector2.autoRequeue();
                selector2.start();
            }
        },"t2");
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                CuratorFramework client3 = getZkCurator();
                client3.start();
                LeaderSelector selector3 = new LeaderSelector(client3,
                        master_path,
                        new LeaderSelectorListenerAdapter() {
                            @Override
                            public void takeLeadership(CuratorFramework client) throws Exception {
                                logger.info("t3成为Master角色");
                                sleep();
                                logger.info("t3完成Master操作，释放Master权利");
                            }
                        });
                selector3.autoRequeue();
                selector3.start();
            }
        },"t3");

        t1.start();
        t2.start();
        t3.start();

        sleep();
    }
}
